package com.example.databasehomework;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Activity_EditUserInfo extends AppCompatActivity {

    private EditText psw_old, psw_1, psw_2, email;
    private Button exit, edit;
    private ImageView exitImg;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_info);
        getViews();
        setViews();
        setListeners();
    }

    public void getViews() {
        psw_old = findViewById(R.id.editinfo_a_num);
        psw_1 = findViewById(R.id.editinfo_b_num);
        psw_2 = findViewById(R.id.editinfo_b_num_sure);
        email = findViewById(R.id.editinfo_c_num);
        edit = findViewById(R.id.editinfo);
        exit = findViewById(R.id.editinfo_returnToHome);
        exitImg = findViewById(R.id.editinfo_head_img);
    }

    public void setViews() {
        intent = getIntent();
        email.setText(intent.getStringExtra("email"));
    }

    public void setListeners() {
        exitImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editUserInfo();
            }
        });
    }

    public void editUserInfo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int ifEqual;
                String password = psw_1.getText().toString();
                if (!password.equals(psw_2.getText().toString())) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getBaseContext(), "两次输入密码不一致", Toast.LENGTH_SHORT).show();
                        }
                    });
                    return;
                }
                int success = HttpUnit.modifySelfInfo(intent.getStringExtra("usn"), password,
                        email.getText().toString(), psw_old.getText().toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (success == -1) {
                            Toast.makeText(getBaseContext(), "修改失败", Toast.LENGTH_SHORT).show();
                        } else if (success == 1) {
                            Toast.makeText(getBaseContext(), "旧密码错误", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getBaseContext(), "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                });
            }
        }).start();
    }
}